﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

using PriErp.Tools.Extensions;

namespace PriErp.Commands
{
    public partial class DotPager : UserControl
    {
        public DotPager()
        {
            InitializeComponent();
        }

        private string Con_Message = "共有记录{0},每页{1}条,第{2}/{3}页";

        private int _pageSize = 10;
        /// <summary>
        /// 分页数
        /// </summary>
        [Description("设置获取分页数")]
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (value >= 1)
                {
                    _pageSize = value;
                    SetControlStatus();
                }
            }
        }

        private int _recordCount;
        /// <summary>
        /// 设置获取记录数
        /// </summary>
        [Description("设置获取记录数")]
        public int RecordCount
        {
            get { return _recordCount; }
            set 
            {
                if (value >= 0)
                {
                    _recordCount = value;
                    SetControlStatus();
                }
            }
        }

        private int _currentPageIndex = 1;
        /// <summary>
        /// 设置获取当前页码
        /// </summary>
        [Description("设置获取当前页码")]
        public int CurrentPageIndex
        {
            get
            {
                return _currentPageIndex < PageCount ? _currentPageIndex : PageCount;
            }
            set 
            {
                int cpage = value;
                if (cpage < 1)
                    cpage = 1;
                else if (cpage > PageCount)
                    cpage = PageCount;
                _currentPageIndex = cpage;
                PageChanging(null, EventArgs.Empty);
            }
        }

        /// <summary>
        /// 获取总页码
        /// </summary>
        [Description("获取总页码")]
        public int PageCount
        {
            get
            {
                int iCount = 1;
                if (RecordCount > 0)
                {
                    iCount = RecordCount / PageSize;
                    if (RecordCount % PageSize != 0)
                        iCount++;
                }
                return iCount;
            }
        }
        /// <summary>
        /// 当前页起始记录数
        /// </summary>
        public int StartRecordIndex
        {
            get
            {
                return (CurrentPageIndex - 1) * PageSize + 1;
            }
        }

        /// <summary>
        /// 剩余记录数
        /// </summary>
        public int RecordsRemain
        {
            get
            {
                return CurrentPageIndex < PageCount ? RecordCount - (CurrentPageIndex * PageSize) : 0;
            }
        }
        /// <summary>
        /// 当前页结束记录数
        /// </summary>
        public int EndRecordIndex
        {
            get
            {
                return RecordCount - RecordsRemain;
            }
        }

        /// <summary>
        /// 翻页事件
        /// </summary>
        [Description("翻页事件")]
        public event EventHandler OnPageChanging;

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PageChanging(object sender, EventArgs e)
        {
            if (OnPageChanging != null)
                OnPageChanging(sender, e);
            SetControlStatus();
        }

        /// <summary>
        /// 设置各按钮状态
        /// </summary>
        private void SetControlStatus()
        {
            if (palAction.InvokeRequired)
            {
                this.Invoke(new Action(SetControlStatus));
                return;
            }
            this.txtPageIndex.Text = string.Empty;
            this.lblMsg.Text = string.Format(Con_Message, _recordCount, _pageSize, CurrentPageIndex, PageCount);
            this.btnFirst.Enabled = this.btnPreview.Enabled = _currentPageIndex > 1;
            this.btnLast.Enabled = this.btnNext.Enabled = _currentPageIndex < PageCount;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = txtPageIndex.Text.ToInt32(1);
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = PageCount;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = 1;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            CurrentPageIndex--;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            CurrentPageIndex++;
        }
    }
}
